home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH9 / SRC / AXON.FRM (.txt) next >
Encoding:
Visual Basic Form  |  1995-11-16  |  12.1 KB  |  403 lines

  1. VERSION 4.00
  2. Begin VB.Form AxonForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Axonometric Orthographic Projection"
  6.    ClientHeight    =   5415
  7.    ClientLeft      =   1215
  8.    ClientTop       =   1005
  9.    ClientWidth     =   6750
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6105
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1155
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5415
  25.    ScaleWidth      =   6750
  26.    Top             =   375
  27.    Width           =   6870
  28.    Begin VB.PictureBox Pict 
  29.       AutoRedraw      =   -1  'True
  30.       Height          =   2175
  31.       Index           =   5
  32.       Left            =   4560
  33.       ScaleHeight     =   -10
  34.       ScaleLeft       =   -5
  35.       ScaleMode       =   0  'User
  36.       ScaleTop        =   5
  37.       ScaleWidth      =   10
  38.       TabIndex        =   10
  39.       Top             =   2760
  40.       Width           =   2175
  41.    End
  42.    Begin VB.PictureBox Pict 
  43.       AutoRedraw      =   -1  'True
  44.       Height          =   2175
  45.       Index           =   4
  46.       Left            =   2280
  47.       ScaleHeight     =   -10
  48.       ScaleLeft       =   -5
  49.       ScaleMode       =   0  'User
  50.       ScaleTop        =   5
  51.       ScaleWidth      =   10
  52.       TabIndex        =   4
  53.       Top             =   2760
  54.       Width           =   2175
  55.    End
  56.    Begin VB.PictureBox Pict 
  57.       AutoRedraw      =   -1  'True
  58.       Height          =   2175
  59.       Index           =   3
  60.       Left            =   0
  61.       ScaleHeight     =   -10
  62.       ScaleLeft       =   -5
  63.       ScaleMode       =   0  'User
  64.       ScaleTop        =   5
  65.       ScaleWidth      =   10
  66.       TabIndex        =   3
  67.       Top             =   2760
  68.       Width           =   2175
  69.    End
  70.    Begin VB.PictureBox Pict 
  71.       AutoRedraw      =   -1  'True
  72.       Height          =   2175
  73.       Index           =   2
  74.       Left            =   4560
  75.       ScaleHeight     =   -10
  76.       ScaleLeft       =   -5
  77.       ScaleMode       =   0  'User
  78.       ScaleTop        =   5
  79.       ScaleWidth      =   10
  80.       TabIndex        =   2
  81.       Top             =   0
  82.       Width           =   2175
  83.    End
  84.    Begin VB.PictureBox Pict 
  85.       AutoRedraw      =   -1  'True
  86.       Height          =   2175
  87.       Index           =   1
  88.       Left            =   2280
  89.       ScaleHeight     =   -10
  90.       ScaleLeft       =   -5
  91.       ScaleMode       =   0  'User
  92.       ScaleTop        =   5
  93.       ScaleWidth      =   10
  94.       TabIndex        =   1
  95.       Top             =   0
  96.       Width           =   2175
  97.    End
  98.    Begin VB.PictureBox Pict 
  99.       AutoRedraw      =   -1  'True
  100.       Height          =   2175
  101.       Index           =   0
  102.       Left            =   0
  103.       ScaleHeight     =   -10
  104.       ScaleLeft       =   -5
  105.       ScaleMode       =   0  'User
  106.       ScaleTop        =   5
  107.       ScaleWidth      =   10
  108.       TabIndex        =   0
  109.       Top             =   0
  110.       Width           =   2175
  111.    End
  112.    Begin VB.Label Label1 
  113.       Alignment       =   2  'Center
  114.       Caption         =   "The final projection"
  115.       Height          =   255
  116.       Index           =   5
  117.       Left            =   4560
  118.       TabIndex        =   11
  119.       Top             =   5040
  120.       Width           =   2175
  121.       WordWrap        =   -1  'True
  122.    End
  123.    Begin VB.Label Label1 
  124.       Alignment       =   2  'Center
  125.       Caption         =   "Project"
  126.       Height          =   255
  127.       Index           =   4
  128.       Left            =   2280
  129.       TabIndex        =   9
  130.       Top             =   5040
  131.       Width           =   2175
  132.       WordWrap        =   -1  'True
  133.    End
  134.    Begin VB.Label Label1 
  135.       Alignment       =   2  'Center
  136.       Caption         =   "Rotate into Y axis"
  137.       Height          =   255
  138.       Index           =   3
  139.       Left            =   0
  140.       TabIndex        =   8
  141.       Top             =   5040
  142.       Width           =   2175
  143.       WordWrap        =   -1  'True
  144.    End
  145.    Begin VB.Label Label1 
  146.       Alignment       =   2  'Center
  147.       Caption         =   "Rotate into Y-Z plane"
  148.       Height          =   255
  149.       Index           =   2
  150.       Left            =   4560
  151.       TabIndex        =   7
  152.       Top             =   2280
  153.       Width           =   2175
  154.       WordWrap        =   -1  'True
  155.    End
  156.    Begin VB.Label Label1 
  157.       Alignment       =   2  'Center
  158.       Caption         =   "Translate to origin"
  159.       Height          =   255
  160.       Index           =   1
  161.       Left            =   2280
  162.       TabIndex        =   6
  163.       Top             =   2280
  164.       Width           =   2175
  165.       WordWrap        =   -1  'True
  166.    End
  167.    Begin VB.Label Label1 
  168.       Alignment       =   2  'Center
  169.       Caption         =   "Original picture"
  170.       Height          =   255
  171.       Index           =   0
  172.       Left            =   0
  173.       TabIndex        =   5
  174.       Top             =   2280
  175.       Width           =   2175
  176.       WordWrap        =   -1  'True
  177.    End
  178.    Begin VB.Menu mnuFile 
  179.       Caption         =   "&File"
  180.       Begin VB.Menu mnuFileExit 
  181.          Caption         =   "E&xit"
  182.       End
  183.    End
  184. Attribute VB_Name = "AxonForm"
  185. Attribute VB_Creatable = False
  186. Attribute VB_Exposed = False
  187. Option Explicit
  188. ' Location of viewing eye.
  189. Dim EyeR As Single
  190. Dim EyeTheta As Single
  191. Dim EyePhi As Single
  192. ' Location of focus point.
  193. Const FocusX = 0#
  194. Const FocusY = 0#
  195. Const FocusZ = 0#
  196. Dim Projector(1 To 4, 1 To 4) As Single
  197. ' The transformation matrices.
  198. Dim M(0 To 4) As Transformation
  199. ' First segment not in the axes.
  200. Dim FirstSegment As Integer
  201. ' ***********************************************
  202. ' Create the matrices used when performing an
  203. ' axonometric orthographic projection with focus
  204. ' at (f1, f2, f3) and projection direction
  205. ' <n1, n2, n3>.
  206. ' ***********************************************
  207. Sub CreateMatrices(f1 As Single, f2 As Single, f3 As Single, n1 As Single, n2 As Single, n3 As Single)
  208. Dim Trans(1 To 4, 1 To 4) As Single
  209. Dim Rot1(1 To 4, 1 To 4) As Single
  210. Dim Rot2(1 To 4, 1 To 4) As Single
  211. Dim Proj(1 To 4, 1 To 4) As Single
  212. Dim D As Single
  213. Dim L As Single
  214.     ' Translate the focus point to the origin.
  215.     m3Translate Trans, -f1, -f2, -f3
  216.     ' Rotate around Z-axis until the projection
  217.     ' direction is in the Y-Z plane.
  218.     m3Identity Rot1
  219.     D = Sqr(n1 * n1 + n2 * n2)
  220.     Rot1(1, 1) = n2 / D
  221.     Rot1(1, 2) = n1 / D
  222.     Rot1(2, 1) = -Rot1(1, 2)
  223.     Rot1(2, 2) = Rot1(1, 1)
  224.     ' Rotate around the X-axis until the normal
  225.     ' lies along the Y axis.
  226.     m3Identity Rot2
  227.     L = Sqr(n1 * n1 + n2 * n2 + n3 * n3)
  228.     Rot2(2, 2) = D / L
  229.     Rot2(2, 3) = -n3 / L
  230.     Rot2(3, 2) = -Rot2(2, 3)
  231.     Rot2(3, 3) = Rot2(2, 2)
  232.     ' Project into the X-Z plane.
  233.     m3Identity Proj
  234.     Proj(2, 2) = 0
  235.     ' Put the matrices in the M array.
  236.     m3Identity M(0).M
  237.     m3MatCopy M(1).M, Trans
  238.     m3MatCopy M(2).M, Rot1
  239.     m3MatCopy M(3).M, Rot2
  240.     m3MatCopy M(4).M, Proj
  241. End Sub
  242. ' ***********************************************
  243. ' Let the user change the location of the eye.
  244. ' ***********************************************
  245. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  246. Const Dtheta = PI / 20
  247.     Select Case KeyCode
  248.         Case vbKeyLeft
  249.             EyeTheta = EyeTheta - Dtheta
  250.             
  251.         Case vbKeyRight
  252.             EyeTheta = EyeTheta + Dtheta
  253.         
  254.         Case vbKeyUp
  255.             EyePhi = EyePhi - Dtheta
  256.         
  257.         Case vbKeyDown
  258.             EyePhi = EyePhi + Dtheta
  259.         
  260.         Case Else
  261.             Exit Sub
  262.     End Select
  263.     ' Make a new projection matrix.
  264.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  265.     ' Redraw the pictures.
  266.     DrawAllData
  267. End Sub
  268. ' *******************************************************
  269. ' Rotate the points in the cube and draw the cube.
  270. ' *******************************************************
  271. Private Sub DrawTheData(pic As Object, project As Boolean)
  272. Dim i As Integer
  273. Dim x1 As Single
  274. Dim y1 As Single
  275. Dim x2 As Single
  276. Dim y2 As Single
  277. Dim oldwidth As Integer
  278.     ' If we should project, do so.
  279.     If project Then TransformData Projector, 1, NumSegments
  280.     ' Draw the points.
  281.     pic.Cls
  282.     oldwidth = pic.DrawWidth
  283.     For i = 1 To NumSegments
  284.         x1 = Segments(i).fr_tr(1)
  285.         y1 = Segments(i).fr_tr(2)
  286.         x2 = Segments(i).to_tr(1)
  287.         y2 = Segments(i).to_tr(2)
  288.         
  289.         ' Draw the plane's normal in bold.
  290.         If i = 4 Then pic.DrawWidth = 3
  291.         pic.Line (x1, y1)-(x2, y2)
  292.         If i = 4 Then pic.DrawWidth = oldwidth
  293.     Next i
  294.     pic.Refresh
  295. End Sub
  296. Private Sub Form_Load()
  297.     ' Initialize the eye position.
  298.     EyeR = 3
  299.     EyeTheta = PI * 0.4
  300.     EyePhi = PI * 0.2
  301.     ' Create the initial viewing transformation.
  302.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  303.     ' Create the projection matrices.
  304.     CreateMatrices 2, 2, 2, 2, 1, 1
  305.     ' Create, project, and draw the data.
  306.     DrawAllData
  307. End Sub
  308. ' ***********************************************
  309. ' Draw all the pictures.
  310. ' ***********************************************
  311. Sub DrawAllData()
  312. Dim i As Integer
  313. Dim P(1 To 4, 1 To 4) As Single
  314.     ' Start with fresh data.
  315.     CreateData
  316.     For i = 0 To 4
  317.         ' Apply the next transformation.
  318.         TransformData M(i).M, FirstSegment, NumSegments
  319.         SetPoints FirstSegment, NumSegments
  320.         
  321.         ' Display the data.
  322.         DrawTheData Pict(i), True
  323.     Next i
  324.     ' Create the final, transformed picture.
  325.     m3OrthoTop P
  326.     TransformData P, 1, NumSegments
  327.     DrawTheData Pict(5), False
  328. End Sub
  329. Sub CreateData()
  330. Dim L As Single
  331. Dim v1x As Single
  332. Dim v1y As Single
  333. Dim v1z As Single
  334. Dim v2x As Single
  335. Dim v2y As Single
  336. Dim v2z As Single
  337. Dim p1x As Single
  338. Dim p1y As Single
  339. Dim p1z As Single
  340. Dim p2x As Single
  341. Dim p2y As Single
  342. Dim p2z As Single
  343. Dim p3x As Single
  344. Dim p3y As Single
  345. Dim p3z As Single
  346. Dim p4x As Single
  347. Dim p4y As Single
  348. Dim p4z As Single
  349.     ' Start with no data.
  350.     NumSegments = 0
  351.     ' Create the axes.
  352.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  353.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  354.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  355.     FirstSegment = NumSegments + 1
  356.     ' Make a projection direction vector.
  357.     MakeSegment 2, 2, 2, 4, 3, 3
  358.     ' Create the edges of the projection plane.
  359.     m3Cross v1x, v1y, v1z, 2, 1, 1, 0, 1, 0
  360.     L = Sqr(v1x * v1x + v1y * v1y + v1z * v1z)
  361.     v1x = 3 * v1x / L
  362.     v1y = 3 * v1y / L
  363.     v1z = 3 * v1z / L
  364.     m3Cross v2x, v2y, v2z, 2, 1, 1, v1x, v1y, v1z
  365.     L = Sqr(v2x * v2x + v2y * v2y + v2z * v2z)
  366.     v2x = 3 * v2x / L
  367.     v2y = 3 * v2y / L
  368.     v2z = 3 * v2z / L
  369.     p1x = 2 + v1x + v2x
  370.     p1y = 2 + v1y + v2y
  371.     p1z = 2 + v1z + v2z
  372.     p2x = 2 - v1x + v2x
  373.     p2y = 2 - v1y + v2y
  374.     p2z = 2 - v1z + v2z
  375.     p3x = 2 - v1x - v2x
  376.     p3y = 2 - v1y - v2y
  377.     p3z = 2 - v1z - v2z
  378.     p4x = 2 + v1x - v2x
  379.     p4y = 2 + v1y - v2y
  380.     p4z = 2 + v1z - v2z
  381.     MakeSegment p1x, p1y, p1z, p2x, p2y, p2z
  382.     MakeSegment p2x, p2y, p2z, p3x, p3y, p3z
  383.     MakeSegment p3x, p3y, p3z, p4x, p4y, p4z
  384.     MakeSegment p4x, p4y, p4z, p1x, p1y, p1z
  385.     ' Create a cube to project.
  386.     MakeSegment 1, 1, 1, 1, 3, 1
  387.     MakeSegment 1, 3, 1, 3, 3, 1
  388.     MakeSegment 3, 3, 1, 3, 1, 1
  389.     MakeSegment 3, 1, 1, 1, 1, 1
  390.     MakeSegment 1, 1, 3, 1, 3, 3
  391.     MakeSegment 1, 3, 3, 3, 3, 3
  392.     MakeSegment 3, 3, 3, 3, 1, 3
  393.     MakeSegment 3, 1, 3, 1, 1, 3
  394.     MakeSegment 1, 1, 1, 1, 1, 3
  395.     MakeSegment 1, 3, 1, 1, 3, 3
  396.     MakeSegment 3, 3, 1, 3, 3, 3
  397.     MakeSegment 3, 1, 1, 3, 1, 3
  398.     NumSegments = NumSegments
  399. End Sub
  400. Private Sub mnuFileExit_Click()
  401.     Unload Me
  402. End Sub
  403.